Generative AI Through Computer Vision¶

In [1]:
import numpy as np
In [2]:
ones_arr = np.ones((5,5))
In [3]:
ones_arr
Out[3]:
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
In [4]:
ones_arr = np.ones((5,5), dtype = int)
ones_arr
Out[4]:
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])
In [5]:
zeros_arr = np.zeros((3,3), dtype = int)
In [6]:
zeros_arr
Out[6]:
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])
In [7]:
ones_arr
Out[7]:
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])
In [8]:
zeros_arr*255
Out[8]:
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])
In [9]:
import matplotlib.pyplot as plt
from PIL import Image
In [10]:
%matplotlib inline
In [11]:
Sunflower_img = Image.open(r'E:\Users\Public\Pictures\sunflower.jpg')
In [12]:
type(Sunflower_img)
Out[12]:
PIL.JpegImagePlugin.JpegImageFile
In [13]:
Sunflower_arr = np.asarray(Sunflower_img)
In [14]:
type(Sunflower_arr)
Out[14]:
numpy.ndarray
In [15]:
Sunflower_arr.shape
Out[15]:
(5461, 8192, 3)
In [16]:
plt.imshow(Sunflower_arr)
Out[16]:
<matplotlib.image.AxesImage at 0x1a6ba502b00>
In [17]:
Sunflower_red = Sunflower_arr.copy()
#plt.imshow
In [18]:
plt.imshow
Out[18]:
<function matplotlib.pyplot.imshow(X, cmap=None, norm=None, *, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, interpolation_stage=None, filternorm=True, filterrad=4.0, resample=None, url=None, data=None, **kwargs)>
In [19]:
Sunflower_red.shape
Out[19]:
(5461, 8192, 3)
In [20]:
plt.imshow(Sunflower_red[:, :, 0])
Out[20]:
<matplotlib.image.AxesImage at 0x1a6d4ffe710>
In [21]:
plt.imshow(Sunflower_red[:, :, 0], cmap = 'gray')
Out[21]:
<matplotlib.image.AxesImage at 0x1a6e9ca9990>
In [22]:
plt.imshow(Sunflower_red[:, :, 1], cmap = 'PuBu')
Out[22]:
<matplotlib.image.AxesImage at 0x1a6e9d1f940>
In [23]:
plt.imshow(Sunflower_red[:, :, 2], cmap= 'autumn')
Out[23]:
<matplotlib.image.AxesImage at 0x1a6ef31e6e0>
In [24]:
plt.imshow(Sunflower_red[:,:, 1], cmap= 'BuGn')
Out[24]:
<matplotlib.image.AxesImage at 0x1a6ef3a1240>
In [25]:
plt.imshow(Sunflower_red)
Out[25]:
<matplotlib.image.AxesImage at 0x1a6f498bc10>
In [26]:
arr1 = np.asarray(Sunflower_img)
In [27]:
type(arr1)
Out[27]:
numpy.ndarray
In [28]:
arr1.shape
Out[28]:
(5461, 8192, 3)
In [29]:
Sunflower_img1 = arr1.copy()
In [30]:
Sunflower_img1
Out[30]:
array([[[114, 173, 217],
        [111, 170, 214],
        [109, 168, 212],
        ...,
        [ 16,  47,  42],
        [ 27,  58,  53],
        [ 16,  47,  42]],

       [[114, 173, 217],
        [111, 170, 214],
        [109, 168, 212],
        ...,
        [ 17,  48,  43],
        [ 23,  54,  49],
        [ 19,  50,  45]],

       [[113, 172, 216],
        [111, 170, 214],
        [110, 169, 213],
        ...,
        [ 17,  48,  43],
        [ 16,  47,  42],
        [ 22,  53,  48]],

       ...,

       [[ 54,  85,  88],
        [ 57,  88,  91],
        [ 58,  89,  92],
        ...,
        [ 28,  40,   2],
        [ 33,  45,   9],
        [ 45,  57,  21]],

       [[ 65,  96,  99],
        [ 64,  95,  98],
        [ 60,  91,  94],
        ...,
        [ 19,  31,   0],
        [ 28,  40,   4],
        [ 46,  58,  22]],

       [[ 71, 102, 105],
        [ 67,  98, 101],
        [ 57,  88,  91],
        ...,
        [ 35,  47,   9],
        [ 28,  40,   4],
        [ 26,  38,   2]]], dtype=uint8)
In [31]:
!pip install opencv-python
import matplotlib.pyplot as plt
%matplotlib inline
import cv2
Requirement already satisfied: opencv-python in c:\users\hp\downloads\new folder\envs\tensorflow_env\lib\site-packages (4.8.0.76)
Requirement already satisfied: numpy>=1.21.2 in c:\users\hp\downloads\new folder\envs\tensorflow_env\lib\site-packages (from opencv-python) (1.24.3)
In [32]:
img= cv2.imread(r'E:\Users\Public\Pictures\sunflower.jpg')
In [33]:
type(img)
Out[33]:
numpy.ndarray
In [34]:
img.shape
Out[34]:
(5461, 8192, 3)
In [35]:
fix_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
In [36]:
fix_img
Out[36]:
array([[[114, 173, 217],
        [111, 170, 214],
        [109, 168, 212],
        ...,
        [ 16,  47,  42],
        [ 27,  58,  53],
        [ 16,  47,  42]],

       [[114, 173, 217],
        [111, 170, 214],
        [109, 168, 212],
        ...,
        [ 17,  48,  43],
        [ 23,  54,  49],
        [ 19,  50,  45]],

       [[113, 172, 216],
        [111, 170, 214],
        [110, 169, 213],
        ...,
        [ 17,  48,  43],
        [ 16,  47,  42],
        [ 22,  53,  48]],

       ...,

       [[ 54,  85,  88],
        [ 57,  88,  91],
        [ 58,  89,  92],
        ...,
        [ 28,  40,   2],
        [ 33,  45,   9],
        [ 45,  57,  21]],

       [[ 65,  96,  99],
        [ 64,  95,  98],
        [ 60,  91,  94],
        ...,
        [ 19,  31,   0],
        [ 28,  40,   4],
        [ 46,  58,  22]],

       [[ 71, 102, 105],
        [ 67,  98, 101],
        [ 57,  88,  91],
        ...,
        [ 35,  47,   9],
        [ 28,  40,   4],
        [ 26,  38,   2]]], dtype=uint8)
In [37]:
plt.imshow(fix_img)
Out[37]:
<matplotlib.image.AxesImage at 0x1a69109ae90>
In [38]:
img_gray= cv2.imread(r'E:\Users\Public\Pictures\sunflower.jpg',cv2.IMREAD_GRAYSCALE)
In [39]:
img_gray.shape
Out[39]:
(5461, 8192)
In [40]:
img_gray.min()
Out[40]:
0
In [41]:
img_gray.max()
Out[41]:
255
In [42]:
plt.imshow(img_gray)
Out[42]:
<matplotlib.image.AxesImage at 0x1a6e9d66650>
In [43]:
fix_img.shape
Out[43]:
(5461, 8192, 3)
In [44]:
plt.imshow(img_gray,cmap= 'magma')
Out[44]:
<matplotlib.image.AxesImage at 0x1a6a9316d40>
In [45]:
plt.imshow(img_gray,cmap= 'gray')
Out[45]:
<matplotlib.image.AxesImage at 0x1a6a9398d90>
In [46]:
fix_img_1 = cv2.resize(fix_img,(500,367))
In [47]:
w_ratio = 0.5
h_ratio = 0.5
fix_img_2 = cv2.resize(fix_img, (0,0), fix_img, w_ratio,h_ratio)
In [48]:
img3 = cv2.flip(fix_img_2,0)
In [49]:
plt.imshow(img3)
Out[49]:
<matplotlib.image.AxesImage at 0x1a6abeec910>
In [50]:
cv2.imwrite('new genai image.jpg', img3)
Out[50]:
True

By using Matplotlib we will make image with opencv¶

In [59]:
import cv2
In [60]:
myimg =cv2.imread(r'C:\Users\HP\OneDrive\Pictures\shivani pic.jpeg')
In [61]:
cv2.imshow('myimg', myimg)
In [ ]:
cv2.waitKey()
In [ ]:
import cv2
while True:
     cv2.imshow('myimg', myimg)
    if cv2.waitKey(1) & 0XFF ==27:
            break
cv2.destroyAllWindow()

Lets read the videos with open cv¶

In [ ]:
import cv2

cap = cv2.VideoCapture(0)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    cv2.imshow('frame', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
In [ ]:
 
In [ ]:
 
In [ ]: